/*
 * This packet is a select an opponent.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class OppChoicePacket extends Packet {

    boolean wait;
    short options;
    String user_name;
    
    public OppChoicePacket( short game_name, short game_version, boolean wait,
                            String user_name ) throws PacketException {
        
        /*Set up the header*/
        message_type = MSG_OPP_CHOICE;
        this.game_name = game_name;
        this.game_version = game_version;
        
        /*fill in rest of packet*/
        if ( user_name.length() > 30 ) {
            throw new PacketException( "Invalid user length (must be <30)" );
        } 
        this.user_name = user_name;
        
        this.wait = wait;
        
        data_area_size = 4 + 30*2;
    }
    
    public OppChoicePacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        
        options = buf.getShort();
        if ( options == 0x1 ) wait = true;
        else wait = false;
        
        short empty = buf.getShort();
        
        /*get user name*/
        char[] un = new char[30];
        for ( int i = 0; i < 30; i++ )
            un[i] = buf.getChar();
        user_name = new String(un);
    }
	
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        
        if ( wait ) options = 0x1;
        else options = 0x0;
        
        buf.putShort( options );
        buf.putShort( (short)0 );
        
        /*put in the username*/
        int i = 0;
        for ( ; i < user_name.length(); i++ )
            buf.putChar( user_name.charAt( i ) );
        for ( ; i < 30; i++ ) 
            buf.putChar( '\0' );
        
        return buf.array();
    }
    
    public String toString() {
        String ret = super.toString() + "\nwait = " + wait;
        ret = ret + "\nuser_name=" + user_name;
        return ret;
    }
    
    public boolean get_wait() { return wait; }
    public short get_options() { return options; }
    public String get_user_name() { return user_name; }
    
}
